library(tidyverse)
library(psych)

rescale.fun <- function(x){(x-min(x, na.rm = T))/diff(range(x, na.rm = T))}

setwd("")

data <- read.csv("Who is the Party - full data for replication archive.csv", na.strings = c("", "NA"))

##Drop responses that reject consent or recruitment

data <- data[which(data$recruit == 1),]
data <- data[which(data$consent == 1),]

##Marking treatment observations with < 8 second on first profile

data$inattentive <- 0

data$inattentive[which(data$timeCommonDem_Page.Submit < 8)] <- 1

data$inattentive[which(data$timeCommonRep_Page.Submit < 8)] <- 1

##Makes missing PID into NA

data$pid7[which(data$pid7 == "political_party")] <- NA

data$pid7 <- as.numeric(data$pid7)

##Check alpha of interestIndex

alpha(data[c("interest1", "interest2")])

##Create normalized feeling thermometer

data$meanTherm <- (data$feelThermPost_3 + data$feelThermPost_4 + data$feelThermPost_5 + data$feelThermPost_6)/4

data$repThermNorm <- data$feelThermPost_1 - data$meanTherm

data$demThermNorm <- data$feelThermPost_2 - data$meanTherm

##Now recoding trust, testing and creating index

data$repTrust1 <- NA

data$repTrust2 <- NA

data$demTrust1 <- NA

data$demTrust2 <- NA

data$repTrust1[which(is.na(data$repTrust1.1) == T)] <- data$repTrust1.2[which(is.na(data$repTrust1.1) == T)] 

data$repTrust1[which(is.na(data$repTrust1.2) == T)] <- data$repTrust1.1[which(is.na(data$repTrust1.2) == T)] 

data$demTrust1[which(is.na(data$demTrust1.1) == T)] <- data$demTrust1.2[which(is.na(data$demTrust1.1) == T)] 

data$demTrust1[which(is.na(data$demTrust1.2) == T)] <- data$demTrust1.1[which(is.na(data$demTrust1.2) == T)] 

data$repTrust2[which(is.na(data$repTrust2.1) == T)] <- data$repTrust2.2[which(is.na(data$repTrust2.1) == T)] 

data$repTrust2[which(is.na(data$repTrust2.2) == T)] <- data$repTrust2.1[which(is.na(data$repTrust2.2) == T)] 

data$demTrust2[which(is.na(data$demTrust2.1) == T)] <- data$demTrust2.2[which(is.na(data$demTrust2.1) == T)] 

data$demTrust2[which(is.na(data$demTrust2.2) == T)] <- data$demTrust2.1[which(is.na(data$demTrust2.2) == T)] 

alpha(data[c("repTrust1", "repTrust2")])

alpha(data[c("demTrust1", "demTrust2")])

data$repTrustIndex <- (data$repTrust1 + data$repTrust2)/2
data$demTrustIndex <- (data$demTrust1 + data$demTrust2)/2

##Recoding normalized feeling therms and trust indices to 0-1

data$repThermNormRescale <- rescale.fun(data$repThermNorm)
data$demThermNormRescale <- rescale.fun(data$demThermNorm)

data$repTrustIndexRescale <- rescale.fun(data$repTrustIndex)
data$demTrustIndexRescale <- rescale.fun(data$demTrustIndex)

data$repTrustIndexRescale <- 1- data$repTrustIndexRescale
data$demTrustIndexRescale <- 1- data$demTrustIndexRescale

##Calculate alpha for the affect index

alpha(data[c("repThermNormRescale", "repTrustIndexRescale")])
alpha(data[c("demThermNormRescale", "demTrustIndexRescale")])

##Standardized alpha is > .7, so primary analysis will be of the index of these measures

data$repAffectIndex <- (data$repThermNormRescale + data$repTrustIndexRescale)/2
data$demAffectIndex <- (data$demThermNormRescale + data$demTrustIndexRescale)/2

##Now creating social distance measure indices

#First calculating the alpha of the index

alpha(data[c("socDistDems1", "socDistDems2", "socDistDems3")])
alpha(data[c("socDistReps1", "socDistReps2", "socDistReps3")])

#It is higher than .7, so we will analyze this as an index

data$socDistDemsIndex <- (data$socDistDems1 + data$socDistDems2 + data$socDistDems3)/3
data$socDistRepsIndex <- (data$socDistReps1 + data$socDistReps2 + data$socDistReps3)/3

data$socDistDemsIndex <- rescale.fun(data$socDistDemsIndex)
data$socDistRepsIndex <- rescale.fun(data$socDistRepsIndex)

##Recoding policy measures, testing and coding index

data$demTaxes <- NA

data$demTaxes[which(is.na(data$demTaxes1) == T)] <- data$demTaxes2[which(is.na(data$demTaxes1) == T)]

data$demTaxes[which(is.na(data$demTaxes2) == T)] <- data$demTaxes1[which(is.na(data$demTaxes2) == T)]

data$demImmig <- NA

data$demImmig[which(is.na(data$demImmig1) == T)] <- data$demImmig2[which(is.na(data$demImmig1) == T)]

data$demImmig[which(is.na(data$demImmig2) == T)] <- data$demImmig1[which(is.na(data$demImmig2) == T)]

data$demAbortion <- NA

data$demAbortion[which(is.na(data$demAbortion1) == T)] <- data$demAbortion2[which(is.na(data$demAbortion1) == T)]

data$demAbortion[which(is.na(data$demAbortion2) == T)] <- data$demAbortion1[which(is.na(data$demAbortion2) == T)]

data$demIdeo <- NA

data$demIdeo[which(is.na(data$demIdeo1) == T)] <- data$demIdeo2[which(is.na(data$demIdeo1) == T)]

data$demIdeo[which(is.na(data$demIdeo2) == T)] <- data$demIdeo1[which(is.na(data$demIdeo2) == T)]

data$demIdeo <- rescale.fun(data$demIdeo)

data$repIdeo <- NA

data$repIdeo[which(is.na(data$repIdeo1) == T)] <- data$repIdeo2[which(is.na(data$repIdeo1) == T)]

data$repIdeo[which(is.na(data$repIdeo2) == T)] <- data$repIdeo1[which(is.na(data$repIdeo2) == T)]

data$repIdeo <- rescale.fun(data$repIdeo)

data$repTaxes <- NA

data$repTaxes[which(is.na(data$repTaxes1) == T)] <- data$repTaxes2[which(is.na(data$repTaxes1) == T)]

data$repTaxes[which(is.na(data$repTaxes2) == T)] <- data$repTaxes1[which(is.na(data$repTaxes2) == T)]

data$repImmig <- NA

data$repImmig[which(is.na(data$repImmig1) == T)] <- data$repImmig2[which(is.na(data$repImmig1) == T)]

data$repImmig[which(is.na(data$repImmig2) == T)] <- data$repImmig1[which(is.na(data$repImmig2) == T)]

data$repAbortion <- NA

data$repAbortion[which(is.na(data$repAbortion1) == T)] <- data$repAbortion2[which(is.na(data$repAbortion1) == T)]

data$repAbortion[which(is.na(data$repAbortion2) == T)] <- data$repAbortion1[which(is.na(data$repAbortion2) == T)]

data$demTaxes <- rescale.fun(data$demTaxes)
data$demImmig <- rescale.fun(data$demImmig)
data$demAbortion <- rescale.fun(data$demAbortion)

data$repTaxes <- rescale.fun(data$repTaxes)
data$repImmig <- rescale.fun(data$repImmig)
data$repAbortion <- rescale.fun(data$repAbortion)

#Checking the alpha of the indicies created from the three policy measures

alpha(data[c("demTaxes", "demImmig", "demAbortion")])
alpha(data[c("repTaxes", "repImmig", "repAbortion")])

#This is below the pre-registered alpha, so the primary analysis will be of each item individually.
#We calculate the index for use as a non-preregistered analysis.

data$demIssueIndex <- (data$demTaxes + data$demImmig + data$demAbortion)/3
data$repIssueIndex <- (data$repTaxes + data$repImmig + data$repAbortion)/3

#Creating a dataframe with only those variables used in subsequent analyses and writing this to a csv

selected_data <- data %>%
  select(block, inattentive, condition, pid7, interest, 
         repIdeo, repTaxes, repImmig, repAbortion, repIssueIndex, 
         demIdeo, demTaxes, demImmig, demAbortion, demIssueIndex, 
         repAffectIndex, repTrustIndexRescale, repThermNormRescale, socDistRepsIndex, 
         demAffectIndex, demTrustIndexRescale, demThermNormRescale, socDistDemsIndex)

write.csv(selected_data, "Who is the Party - data cleaned for replication archive.csv")




